package com.reekr.a5t.service.impl;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ManagementService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.reekr.a5t.model.activiti.DeploymentVo;
import com.reekr.a5t.model.activiti.ProcessInstanceVo;
import com.reekr.a5t.service.ActivitiService;
import com.reekr.a5t.service.acitiviti.ProcessInstanceDiagramCmd;

@Service
public class ActivitiServiceImpl implements ActivitiService {

	@Autowired
	RepositoryService repositoryService;
	@Autowired
	RuntimeService runtimeService;
	@Autowired
	TaskService taskService;
	@Autowired
	ManagementService managementService;
	
	
	@Override
	public List<DeploymentVo> listDeploymentTop10() {
		List<DeploymentVo> list = DeploymentVo.convert(repositoryService.createDeploymentQuery().list());
		return list;
	}


	@Override
	public boolean deployment() {
		
    	repositoryService.createDeployment().addClasspathResource("bpmn20/ask4leave.bpmn").name("请假流程").category("日常办公").deploy();
		return false;
		
	}


	@Override
	public void clearAll() {
		
		List<ProcessInstance> processlist = runtimeService.createProcessInstanceQuery().list();
        for (ProcessInstance processInstance : processlist) {
        	runtimeService.deleteProcessInstance(processInstance.getId(), "ask4leave");
		}
        List<Deployment> list = repositoryService.createDeploymentQuery().list();
    	for (Deployment deployment : list) {
			System.out.println(deployment.getName());
			repositoryService.deleteDeployment(deployment.getId());
		}
	}


	@Override
	public void initiate() {
		Map<String,Object> agrs = new HashMap<String,Object>();
		agrs.put("title", "张三病假");
		agrs.put("reason", "我的兄弟“杜子腾”来拜访我了！");
		ProcessInstance pi = runtimeService.startProcessInstanceByKey("ask4leave",agrs);
		System.out.println("=====++++=====>"+pi.getProcessInstanceId());
		Task task = taskService.createTaskQuery()
				.processInstanceId(pi.getProcessInstanceId()).singleResult();
		Map<String,Object> agrs1 = new HashMap<String,Object>();
		agrs1.put("title", "张三病假111");
		agrs1.put("reason", "我的兄弟“杜子腾”来拜访我了！11");
		taskService.complete(task.getId(), agrs1);
	}


	@Override
	public List<ProcessInstanceVo> listProcessInstance() {
		List<ProcessInstanceVo> list = ProcessInstanceVo.convert(runtimeService.createProcessInstanceQuery().includeProcessVariables().list());
		for (ProcessInstanceVo processInstanceVo : list) {
			Map<String,Object> map = processInstanceVo.getProcessVariables();
			for (String key : map.keySet()) {
				System.out.println("--->"+key+"="+map.get(key));
			}
		}
		return list;
	}


	@Override
	public InputStream getImage(String processInstanceId) {
		
		Command<InputStream> cmd = new ProcessInstanceDiagramCmd(processInstanceId);
		InputStream is = managementService.executeCommand(cmd);
		return is;
	}

}
